<?php

include_once 'Super_Model_Admin.php';

class Admin_Model_Content extends Super_Model_Admin {

    function getItems(array $qAr, &$count_rows = 0, $serach = NULL) {
        $sql = 'SELECT SQL_CALC_FOUND_ROWS * FROM `' . $qAr['table_name'] . '` t ';
        $sql .= ' WHERE 1 ';
        if (!empty($qAr['title'])) {
            $sql .= ' AND t.title LIKE \'%' . addslashes($qAr['title']) . '%\'';
        }
        if (!empty($qAr['content'])) {
            $sql .= ' AND t.content LIKE \'%' . addslashes($qAr['content']) . '%\'';
        }
        if (!empty($qAr['pages'])) {
            $sql .= ' AND t.page_id = ' . intval($qAr['pages']);
        }
        if (!empty($qAr['type_filter'])) {
            $sql .= ' AND t.type = ' . intval($qAr['type_filter']);
        }
        if(!empty ($serach)){
            $sql .= $serach;
        }
        $sql .= ' ORDER BY t.`' . addslashes($qAr['order_field']) . '` ' . addslashes($qAr['order_dir']);
        $sql .= ' LIMIT ' . (int) $qAr['offset'] . ', ' . (int) $qAr['limit'];
        //_debug($sql);
        $result = $this->GetAll($sql);
        if ($result) {
            $count_rows = $this->GetOne("SELECT FOUND_ROWS() as total");
            return $result;
        }
        return false;
    }

    public function formData(&$data) {
        if (empty($data['url']))
            $data['url'] = $this->createUrl($data['title']);
        if (empty($data['page_title']))
            $data['page_title'] = trim($data['title']);
        if (empty($data['metakeywords']))
            $data['metakeywords'] = trim($data['title']);
        if (empty($data['description']))
            $data['description'] = trim($data['title']);
        if (empty($data['rating']))
            $data['rating'] = 0;
        if (empty($data['preview']))
            $data['preview'] = $this->maxsite_str_word(strip_tags(html_entity_decode($data['content'])), 50);
    }

}